package fi.solita.dojo.mazeserver.ui

import swing._
import swing.event._

import fi.solita.dojo.mazeserver.game._
import fi.solita.dojo.mazeserver.game.internal._

case class GameChangedEvent(map: GameMap, players: PlayerList) extends Event
case class PlayersAddedEvent(dto: PlayerList) extends Event
case class PlayersLeftEvent(dto: PlayerList) extends Event
case class PlayersUpdatedEvent(dto: PlayerList) extends Event
case class ActiveMapChangedEvent(maps: List[GameMap]) extends Event
case class RefreshEvent() extends Event

object ServerUI extends BoxPanel(Orientation.Vertical) with Publisher {


	var mapArea = new GameAreaComponent
	val playerList = new PlayerListComponent

	val resetButton = new Button {
		text = "Reset map (only when stopped)"	
	}
 
	val actButton = new Button {
		text = "Act"	
	}

	val startButton = new Button {
		text = "Start"	
	}
	
	contents += mapArea
	contents += playerList

	contents += resetButton
	contents += actButton
	contents += startButton
 
	border = Swing.EmptyBorder(30, 30, 10, 30)

	listenTo(resetButton)
	listenTo(actButton)
	listenTo(startButton)
	listenTo(GameWorld)

	reactions += {	    
	case ButtonClicked(b) => {
		if (b == resetButton) if (!GameWorld.running) GameActor ! NewMap()
		if (b == actButton) GameActor ! ForceAction()
		if (b == startButton) run()
	}
	}
 
	def run() {
	  if (GameWorld.running) startButton.text = "Start" else startButton.text = "Stop"
	  GameActor ! StartStop()
	}
 
	def refreshUI() {
		publish(RefreshEvent())
	}
}
